package com.dykj.fireFighting.web.base.emergency;

import com.dykj.fireFighting.api.base.emergency.dto.EmergencyPlanTwoDto;
import com.dykj.fireFighting.api.base.emergency.entity.EmergencyPlanTwo;
import com.dykj.fireFighting.api.base.emergency.service.IEmergencyPlanTwoService;
import com.dykj.fireFighting.api.base.emergency.vo.EmergencyPlanTwoVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;


/**
 * 应急预案新版接口
 *
 * @Author JacksonTu
 * @Date 2021-08-26 14:40:38
 */
@Slf4j
@Api(value = "应急预案新版接口", tags = {"应急预案新版接口"})
@RestController
@RequestMapping("/emergencyPlanTwo")
public class EmergencyPlanTwoController {

    @DubboReference
    private IEmergencyPlanTwoService emergencyPlanTwoService;


    /**
     * 应急预案新版列表
     *
     * @return
     */
    @AutoLog(value = "应急预案新版-分页列表查询")
    @ApiOperation(value = "应急预案新版分页列表", notes = "应急预案新版分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<EmergencyPlanTwoVo>> treeGrid(EmergencyPlanTwoDto emergencyPlanTwoDto) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        emergencyPlanTwoDto.setUnitId(loginUserVo.getEnterpriseId().toString());
        PageVo<EmergencyPlanTwoVo> page = emergencyPlanTwoService.queryList(emergencyPlanTwoDto);
        return CommonResult.success(page);
    }

    /**
     * 获取应急预案新版信息
     */
    @AutoLog(value = "应急预案新版-获取应急预案新版信息")
    @ApiOperation(value = "获取应急预案新版信息", notes = "获取应急预案新版信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<EmergencyPlanTwo> info(@PathVariable("id") String id) {
        EmergencyPlanTwo emergencyPlanTwo = emergencyPlanTwoService.getInfoById(id);
        return CommonResult.success(emergencyPlanTwo);
    }

    /**
     * 保存应急预案新版信息
     */
    @AutoLog(value = "应急预案新版-保存应急预案新版信息")
    @ApiOperation(value = "保存应急预案新版信息", notes = "保存应急预案新版信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody EmergencyPlanTwo emergencyPlanTwo) {
        try {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            emergencyPlanTwo.setUnitId(loginUserVo.getEnterpriseId().toString());
            emergencyPlanTwo.setCreateBy(loginUserVo.getId().toString());
            emergencyPlanTwo.setCreateTime(new Date());
            emergencyPlanTwoService.save(emergencyPlanTwo);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改应急预案新版信息
     */
    @AutoLog(value = "应急预案新版-修改应急预案新版信息")
    @ApiOperation(value = "修改应急预案新版信息", notes = "修改应急预案新版信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody EmergencyPlanTwo emergencyPlanTwo) {
        try {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            emergencyPlanTwo.setUpdateBy(loginUserVo.getId().toString());
            emergencyPlanTwo.setUpdateTime(new Date());
            emergencyPlanTwoService.updateById(emergencyPlanTwo);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除应急预案新版信息
     */
    @AutoLog(value = "应急预案新版-删除应急预案新版信息")
    @ApiOperation(value = "删除应急预案新版信息", notes = "删除应急预案新版信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[] ids) {
        try {
            emergencyPlanTwoService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 应急预案新版列表
     *
     * @return
     */
    @AutoLog(value = "应急预案新版-分页列表查询")
    @ApiOperation(value = "应急预案新版分页列表", notes = "应急预案新版分页列表")
    @GetMapping("/allList")
    public CommonResult<PageVo<EmergencyPlanTwoVo>> queryAllList(EmergencyPlanTwoDto emergencyPlanTwoDto) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        emergencyPlanTwoDto.setJkzxUnitId(loginUserVo.getEnterpriseId().toString());
        PageVo<EmergencyPlanTwoVo> page = emergencyPlanTwoService.queryAllList(emergencyPlanTwoDto);
        return CommonResult.success(page);
    }
}
